package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.AddressService;
import cn.itcast.service.OrderService;
import cn.itcast.service.SetmealService;
import cn.itcast.utils.ThreadLocalUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.itcast.constant.MessageConstant.NEED_LOGIN;
import static cn.itcast.constant.MessageConstant.VALIDATECODE_ERROR;

@RestController
@RequestMapping(path = "/order")
public class OrderController {


    @Reference
    private OrderService orderService;

    @Reference
    private SetmealService setmealService;

    @Reference
    private AddressService addressService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 提交预约信息   ---- 会员信息与预约信息
     *
     * @param order
     * @return
     */
    @PostMapping(path = "/submit/{validateCode}")
    public Result submit(@RequestBody Order order,@PathVariable String validateCode) {
        try {
            //1. 校验验证码是否正确

            //从order对象里面取出手机号

            String phone = order.getPhoneNumber();

            String securityCode  = this.redisTemplate.opsForValue().get("VALIDATE_CODE_" + phone);

            if(!validateCode.equals(securityCode)){
                return new Result(false, VALIDATECODE_ERROR);
            }



            //2. 校验通过 , 调用服务提交预约数据提交
            Member member = ThreadLocalUtils.getUser();
            if (member == null) {
                return new Result(false, NEED_LOGIN);
            }
            //设置当前登录用户ID
            order.setMemberId(member.getId());
            //设置预约方式
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            //提交预约数据

            return orderService.submit(order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SERVER_BUSY);
        }
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") String id) {
        try {
            Order order = orderService.findById(id);


            Integer setmealId = order.getSetmealId();
            Setmeal setmeal = setmealService.findById(setmealId);

            Integer addressId = order.getAddressId();
            Address address = addressService.findById(addressId);


            order.setSetmeal(setmeal);
            order.setAddress(address);

            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }


    /**
     * 查询当前登录用户订单列表
     *
     * @return
     */
    @GetMapping(path = "/user/findAll")
    public Result findUserAll() {
        try {
            Member member = ThreadLocalUtils.getUser();
            if(member==null){
                return new Result(false, NEED_LOGIN);
            }

            List<Order> orderList = orderService.findUserAll(member.getId());
            for (Order order : orderList) {
                Integer setmealId = order.getSetmealId();
                Setmeal setmeal = this.setmealService.findById(setmealId);
                order.setSetmeal(setmeal);
            }


            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

}
